import { reactive, toRefs } from 'vue';

interface IinitState {
  // 关联账号弹窗展示
  visible: Boolean;
  // 数据
  data: Object | null;
}

interface IParams {
  initState?: IinitState;
  beforeOpen?: Function;
}

export const useDialog = (params: IParams = {}): any => {
  const state: IinitState | any = reactive(params?.initState || { visible: false, data: null });

  function closeDialog() {
    state.visible = false;
    state.data = null;
  }

  function openDialog(data: any) {
    if (params?.beforeOpen) {
      params.beforeOpen(data);
    }
    state.visible = true;
    state.data = data;
  }

  return {
    ...toRefs(state),
    closeDialog,
    openDialog,
  };
};
